<?php

namespace app\api\controller;
use \app\api\model\Account;
class Base
{

    //注册功能
    public function register()
    {
        //传入学号，姓名，密码注册
        $mobile = input('get.mobile');
        $name = input('get.name');
        $password = input('get.password');
        if (empty($mobile) || empty($name) || empty($password)) {
            output_error('信息填写不完整，请填写完整');
        }
        //规则1：学号唯一
        $data = Account::where('member_mobile','=',$mobile)->find();
        if (!empty($data)) output_error('学号已存在');
        //规则2：密码md5加密
        $data = array();
        $data['member_mobile'] = $mobile;
        $data['member_name'] = $name;
        $data['member_password'] =md5($password);
        $data['member_token'] = '-1';
        $data['member_gender'] = '女';
        $data['add_time'] = time();
        Account::insert($data);
        output_data('1');
    }


    //找回密码
    public function findpass()
    {
        //通过学号，姓名重置密码
        $mobile = input('get.mobile');
        $name = input('get.name');
        $password = input('get.password');
        if (empty($mobile) || empty($name) || empty($password)) {
            output_error('信息填写不完整，请填写完整');
        }

        $condition = array();
        $condition[] = ['member_id','=',$mobile];
        $condition[] = ['member_name','=',$name];
        $data = Account::where($condition)->find();
        if (empty($data)) output_error('姓名与学号不匹配，无法重置密码');
        //规则1：密码md5加密
        Account::where('member_id','=',$data['member_id'])->update(array('member_password'=>md5($password)));
        output_error('1');

    }

    //登录功能
    public function login()
    {
        //通过学号，密码登录，返回Token
        //通过学号，姓名重置密码
        $mobile = input('get.mobile ');
        $password = input('get.password');
        if (empty($mobile )   || empty($password)) {
            output_error('请输入账号或密码');
        }
        $condition = array();
        $condition[] = ['member_mobile ','=',$mobile ];
        $condition[] = ['member_password','=',md5($password)];
        $data = Account::where($condition)->find();
        if (empty($data)) output_error('账号或密码错误');

        //规则1：token由学号+当前时间戳md5加密生成
        $token = md5($mobile .time());
        Account::where('member_id','=',$data['member_id']->update('member_token'));
        output_data($token);

    }

}
